<template>
  <div>
    <!-- 表格 -->
    <el-table border :data="records">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column label="名称" prop="skuName"></el-table-column>
      <el-table-column label="描述" prop="skuDesc"></el-table-column>
      <el-table-column label="默认图片">
        <template slot-scope="{ row, $index }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 80px; height: 80px"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="重量(KG)"
        prop="weight"
        width="80px"
      ></el-table-column>
      <el-table-column
        label="价格(元)"
        prop="price"
        width="80px"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row, $index }">
          <el-tooltip
            class="item"
            effect="dark"
            content="上架"
            placement="top-start"
          >
            <el-button
              type="success"
              icon="el-icon-sort-up"
              size="mini"
              v-show="row.isSale == 0"
              @click="OnSale(row)"
              style="margin-left: 10px"
            >
            </el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="下架"
            placement="top-start"
          >
            <el-button
              type="info"
              icon="el-icon-sort-down"
              size="mini"
              v-show="row.isSale == 1"
              @click="CanelSale(row)"
            >
            </el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="修改"
            placement="top-start"
          >
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="edit"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="查看详情"
            placement="top-start"
          >
            <el-button
              type="info"
              icon="el-icon-info"
              size="mini"
              @click="getSkuInfo(row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top-start"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteSku(row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      style="text-align: center"
      :total="total"
      :current-page="page"
      :page-size="limit"
      :page-sizes="[3, 5, 10]"
      layout="prev,pager,next,jumper,->,sizes,total"
      @current-change="getSkuList"
      @size-change="handleSizeChange"
    ></el-pagination>
    <!-- 抽屉 -->
    <el-drawer :visible.sync="drawer" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5"> 名称 </el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5"> 描述 </el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }} </el-col>
      </el-row>
      <el-row>
        <el-col :span="5"> 价格 </el-col>
        <el-col :span="16">{{ skuInfo.price }}元 </el-col>
      </el-row>
      <el-row>
        <el-col :span="5"> 平台属性 </el-col>
        <el-col :span="16">
          <template>
            <el-tag
              type="success"
              v-for="(attr, index) in skuInfo.skuAttrValueList"
              :key="attr.id"
              style="margin-right: 10px"
              >{{ attr.attrId }}--{{ attr.valueId }}</el-tag
            >
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5"> 商品图片 </el-col>
        <el-col :span="16">
          <el-carousel height="400px">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img :src="item.imgUrl" class="pic" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  mounted() {
    this.getSkuList();
  },
  data() {
    return {
      page: 1,
      limit: 10,
      records: [], //存储sku列表数据
      total: 0, //分页器一共展示的数据
      skuInfo: {}, //sku详细信息
      drawer: false, //抽屉可见性
    };
  },
  methods: {
    //获取sku列表数据
    async getSkuList(pages = 1) {
      this.page = pages;
      const { page, limit } = this;
      let result = await this.$api.sku.reqSkuList(page, limit);
      if (result.code == 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSkuList();
    },
    //上架
    async OnSale(row) {
      let result = await this.$api.sku.reqOnSale(row.id);
      if (result.code == 200) {
        row.isSale = 1;
        this.$message.success("上架成功！");
      }
    },
    //下架
    async CanelSale(row) {
      let result = await this.$api.sku.reqCanelSale(row.id);
      if (result.code == 200) {
        row.isSale = 0;
        this.$message.success("下架成功！");
      }
    },
    edit() {
      this.$message.info("正在开发中！");
    },
    //获取sku详细信息
    async getSkuInfo(row) {
      this.drawer = true;
      let result = await this.$api.sku.reqSkuById(row.id);
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
    },
    //删除sku
    async deleteSku(row){
        let result = await this.$api.sku.reqDeleteSku(row.id);
        if (result.code==200) {
            this.$message.success("删除成功！")
            this.getSkuList();
        }
    }
  },
};
</script>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px 10px;
}
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
.pic {
  display: inline-block;
  height: auto;
  max-width: 100%;
}
</style>
